#include "ForestStatesList.h"
#include "../../dataengine/VorestHtmlReader.h"

#include <QFileDialog>

ForestStatesList::ForestStatesList(QWidget *parent) : QListWidget(parent)
{
	connect(this, SIGNAL(currentRowChanged(int)), this, SLOT(changeSelectedElem(int)));
}

ForestStatesList::~ForestStatesList()
{

}

void ForestStatesList::loadForestStateDialog()
{
	QString fileName;
	bool canLoad = false;
	do{

		QFileDialog::Options options;
		QString selectedFilter;
		fileName = QFileDialog::getOpenFileName(this,
				tr("Select Forest File"),
				QString("."),
				tr("HTML files (*.htm;*.html);;All Files"),
				&selectedFilter,
				options);
		if (fileName.isEmpty())
			return ;
		dataengine::VorestHtmlReader reader(fileName.toStdString());
		canLoad = reader.canLoadFile();
	}while(!canLoad);
	this->addItem(fileName);
	this->setCurrentRow(this->count()-1);
	changeSelectedElem(currentRow());
}

void ForestStatesList::removeCurrentForest()
{
	int current = currentRow();
	this->takeItem(current);
	while(current >= count())
		current--;
	if(current>=0)
		this->setCurrentRow(current);
}

void ForestStatesList::moveCurrentUp()
{
	int current = currentRow();
	if(current == 0)
		return;
	QListWidgetItem * item = takeItem(current);
	this->insertItem(current - 1, item);
	this->setCurrentRow(current - 1);
}

void ForestStatesList::moveCurrentDown()
{
	int current = currentRow();
	if(current == count() -1)
		return;
	QListWidgetItem * item = takeItem(current);
	this->insertItem(current + 1,item);
	this->setCurrentRow(current + 1);
}

void ForestStatesList::changeSelectedElem(int row)
{
	ForestState forest(item(row)->text().toStdString());
	emit selectedForestStateChanged(forest);
}



